package com.bhs.sansong.ui.orderDispose;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.PictureSelectAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.bean.EvaluateBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.LogUtils;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.google.gson.Gson;
import com.willy.ratingbar.BaseRatingBar;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 订单评价
 */
public class OrderEvaluateActivity extends BaseActivity implements View.OnClickListener, NetCallBack, TextWatcher {

    private BaseRatingBar mRbComprehensive;
    private TextView mTvComprehensive;
    private BaseRatingBar mRbServiceAttitude;
    private TextView mTvServiceAttitude;
    private BaseRatingBar mRbQuality;
    private TextView mTvQuality;
    private BaseRatingBar mRbTransportation;
    private TextView mTvTransportation;
    private BaseRatingBar mRbReceipt;
    private TextView mTvReceipt;
    private EditText mEtComment;
    private RecyclerView mRvList;
    private Switch mSwOffer;
    private Button mBtnSubmit;
    private String deliver_sn;
    private PictureSelectAdapter mAdapter;


    @Override
    public int initLayout() {
        return R.layout.activity_order_evaluate;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("公司评价");
        deliver_sn = getIntent().getStringExtra("deliver_sn");
        mRbComprehensive = findViewById(R.id.rb_comprehensive);
        mTvComprehensive = findViewById(R.id.tv_comprehensive);
        mRbServiceAttitude = findViewById(R.id.rb_service_attitude);
        mTvServiceAttitude = findViewById(R.id.tv_service_attitude);
        mRbQuality = findViewById(R.id.rb_quality);
        mTvQuality = findViewById(R.id.tv_quality);
        mRbTransportation = findViewById(R.id.rb_transportation);
        mTvTransportation = findViewById(R.id.tv_transportation);
        mRbReceipt = findViewById(R.id.rb_receipt);
        mTvReceipt = findViewById(R.id.tv_receipt);
        mEtComment = findViewById(R.id.et_comment);
        mRvList = findViewById(R.id.rv_list);
        mSwOffer = findViewById(R.id.sw_offer);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAdapter = new PictureSelectAdapter(3, mActivity);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new GridLayoutManager(mActivity,3));
        mRbServiceAttitude.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                mTvServiceAttitude.setText(String.format("%s(%s)", MyUtils.format2(rating), MyUtils.getScoreGrade(rating)));
                calculate();
                LogUtils.d("???");
            }
        });
        mRbQuality.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                mTvQuality.setText(String.format("%s(%s)", MyUtils.format2(rating), MyUtils.getScoreGrade(rating)));
                calculate();
            }
        });
        mRbReceipt.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                mTvReceipt.setText(String.format("%s(%s)", MyUtils.format2(rating), MyUtils.getScoreGrade(rating)));
                calculate();
            }
        });
        mRbTransportation.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                mTvTransportation.setText(String.format("%s(%s)", MyUtils.format2(rating), MyUtils.getScoreGrade(rating)));
                calculate();
            }
        });
        mEtComment.addTextChangedListener(this);
    }

    private void calculate() {

        float value = (mRbServiceAttitude.getRating() + mRbQuality.getRating() +
                mRbTransportation.getRating() + mRbReceipt.getRating()) / 4;
        mRbComprehensive.setRating(value);
        mTvComprehensive.setText((String.format("%s(%s)", MyUtils.format2(value), MyUtils.getScoreGrade(value))));
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_submit) {
            Map<String, Object> map = new ArrayMap<>();
            map.put("id", deliver_sn);
            map.put("service", mRbServiceAttitude.getRating());
            map.put("waybill", mRbQuality.getRating());
            map.put("transport", mRbTransportation.getRating());
            map.put("receipt", mRbReceipt.getRating());
            map.put("content", mEtComment.getText().toString());
            map.put("anonymous", mSwOffer.isChecked() ? "1" : "0");
            map.put("imgs", mAdapter.getImages());
            networkRequest(netApi.comment_order(map), this);
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        EvaluateBean bean = new Gson().fromJson(json, EvaluateBean.class);
        if (bean.isStatus()) {
            ToastUtil.show("评价成功");
            finish();
        }else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        verify();
    }

    private void verify() {
        boolean verify = true;
        if (TextUtils.isEmpty(mEtComment.getText())) {
            verify = false;
        }
        mBtnSubmit.setEnabled(verify);
        mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }
}